(function ($) {
    /**
     * 获取当前主题
     */
    function getDarkMode() {
        return window.darkMode  ? 'dark' : 'light'
    }

    /**
     * 更新指针主题
     * @param {string}[cursorType] - 指针类型
     */
    function updateCursorTheme(cursorType) {
        const $body = $('body');
        const $root = $(':root');
        const cursorDir = $body.data('cursor-dir');
        const defaultCursor = $body.data('cursor-default');
        if (window.isInitialLoad) {
            $root.css('--custom-cursor', `url("${cursorDir}${defaultCursor}.cur"), auto`);
            $body.addClass("custom-cursor-enabled");
            return;
        }
        if (cursorType === 'None') {
            $root.css('--custom-cursor', 'initial');
            $body.removeClass("custom-cursor-enabled");
        } else {
            $root.css('--custom-cursor', `url("${cursorDir}${cursorType}.cur"), auto`);
            $body.addClass("custom-cursor-enabled");
        }
    }

    /**
     * 校验是否是有效的开关 key
     * @param key
     * @returns {boolean} 校验结果
     */
    function isValidSwitchKey(key) {
        return window.switchConfigs.getAllKeys().includes(key);
    }

    /**
     * 根据控件类型获取控件描述
     * @param {string} type 控件类型
     * @returns {string} 控件描述
     */
    function getControlDesc(type) {
        switch(type) {
            case 'range':
                return '滑块';
            case 'color':
                return '颜色选择器';
            case 'select':
                return '下拉框';
            case 'radio':
                return '单选按钮';
            case 'checkbox':
                return '复选框';
            case 'bootstrap-switch':
                return 'Bootstrap Switch';
            default:
                    return '';
        }
    }

    /**
    * 从本地缓冲 userSettings 中获取指定设置
    * @param {string} key 设置项键名
    * @param {string} type 设置类型，支持 radio, range, color
    * @returns {boolean | number | string} 支持返回状态、数值、颜色或选项值
    */
    function getSettingsFromUserSettings(key, type) {
        // 从本地存储加载设置（匹配图片中的持久化需求）
        const currentSettings = window.AppUtils.getUserSettings();
        const controlDesc = getControlDesc(type);

        // 处理滑块类数值设置
        if (type === 'range') {
            const $range = $(`#${key}`);
            if ($range.length === 0) {
                console.error(`[module] 未找到${controlDesc}控件: #${key}`);
                return 0;
            }
            return window.isInitialLoad && currentSettings && currentSettings[type] !== undefined && currentSettings[type][key] !== undefined
                ? Number.parseInt(currentSettings[type][key])
                : Number.parseInt($range.val());
        } else if (type === 'color' || type === 'select') {
            const $range = $(`#${key}`);
            if ($range.length === 0) {
                console.error(`[module] 未找到${controlDesc}控件: #${key}`);
                return 0;
            }
            return window.isInitialLoad && currentSettings && currentSettings[type] !== undefined && currentSettings[type][key] !== undefined
                ? currentSettings[type][key]
                : $range.val();
        } else if (type === 'radio' || type === 'bootstrap-switch') {
             // 统一校验逻辑
            const getValue = (selector, type) => {
                if (type === 'bootstrap-switch') {
                    return $(`input[name="${selector}"]`).bootstrapSwitch('state');
                } else if (type === 'radio') {
                    return $(`input[name="${selector}"]:checked`).val() === "true";
                } else {
                    console.error(`[module] 不支持从${controlDesc}控件获取值: ${selector}`)
                    return null;
                }
            }

            // 根据设置项处理
            switch(key) {
                case 'displayMode':
                    return window.isInitialLoad && currentSettings && currentSettings.displayMode
                        ? currentSettings.displayMode === 'minimal'
                        : $('input[name="displayMode"]:checked').val() === 'minimal';

                case 'colorTheme':
                    return window.isInitialLoad && currentSettings && currentSettings.colorTheme
                        ? currentSettings.colorTheme === 'dark'
                        : $('input[name="colorTheme"]:checked').val() === 'dark';

                default: {
                    // 统一处理布尔型开关
                    if (isValidSwitchKey(key)) {
                        return window.isInitialLoad && currentSettings && currentSettings[type] !== undefined && currentSettings[type][key] !== undefined
                        ? currentSettings[type][key] === true
                        : getValue(key, type);
                    }
                    console.warn(`[module] 未找到${controlDesc}控件: ${key}`);
                    return false;
                }
            }
        } else {
            console.warn(`[module] 未知控件类型: ${type}`);
            return false;
        }
    }

    window.getDarkMode = getDarkMode;
    window.updateCursorTheme = updateCursorTheme;
    window.getSettingsFromUserSettings = getSettingsFromUserSettings;
})(window.jQuery);